package dom;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.FileOutputStream;
import java.io.IOException;
public class Phone {
    public static void main(String[] args) {
        try {

            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

            DocumentBuilder db = null;
            try {
                db = dbf.newDocumentBuilder();
            } catch (ParserConfigurationException e) {
                e.printStackTrace();
            }

            Document doc= null;//收藏信息.xml
            try {
                doc = db.parse("src/xml/Phone.xml");
            } catch (SAXException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
            //所有Brand节点列表信息
            NodeList brandList=doc.getElementsByTagName("Brand");
            //循环
            for (int i=0; i<brandList.getLength();i++){
                //获得i个Brand元素信息
                Node brand=brandList.item(i);
                //获得第i个Brand元素的name属性的值
                Element element=(Element) brand;
                String attrValue=element.getAttribute("name");
                //获取第i个Brand元素的所有子元素的name属性值
                NodeList types=element.getChildNodes();
                for (int j=0; j<types.getLength();j++){
                    Element typeElement=((Element) types.item(j));  //Type节点
                    String type=typeElement.getAttribute("name");
                    System.out.println("手机:"+attrValue+type);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
